# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import auth
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from sign.models import Event, Guest
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
# Create your views here.
def index(request):
    return render(request, "index.html")

#登录动作
def login_action(request):
    if request.method == "POST":
        username = request.POST.get("username", "")
        password = request.POST.get("password", "")
        user = auth.authenticate(username=username, password=password)
        if user is not None:
            auth.login(request, user) #登录
            request.session["user"] = username #将session 信息记录到浏览器
            response = HttpResponseRedirect("/event_manage/")
            return response
        else:
            return render(request, "index.html", {"error":"username or password error!"})

#发布会管理
@login_required
def event_manage(request):
    event_list = Event.objects.all()
    username = request.session.get("user", "")
    return render(request, "event_manage.html", {"user" : username, "events" : event_list})

#发布会名称搜索
@login_required
def search_name(request):
    username = request.session.get("user","")
    search_name = request.GET.get("name","")
    event_list = Event.objects.filter(name__contains=search_name)
    if event_list is not None:
        event_list=event_list
    else:
        event_list="No"
    return render(request, "event_manage.html", {"user":username,"events":event_list})

#嘉宾管理
@login_required
def guest_manage(request):#
    guest_list = Guest.objects.all()
    username = request.session.get("user","")
    paginator = Paginator(guest_list, 2)#分页每页两行数据
    page = request.GET.get("page")
    try:
        contacts = paginator.page(page)#获取某页面数据
    except PageNotAnInteger:#如果页面不是整数，请发送第一页。
        contacts = paginator.page(1)
    except EmptyPage:
        contacts = paginator.page(paginator.num_pages)

    return render(request, "guest_manage.html", {"user":username, "guests":contacts})

#嘉宾管理名称搜索
@login_required
def search_name(request):
    username = request.session.get("user","")
    search_name = request.GET.get("name","")
    guest_list = Guest.objects.filter(name__contains=search_name)
    return render(request, "guest_manage.html", {"user":username,"events":guest_list})

#签到页面
@login_required
def sign_index(request, event_id):
    event = get_object_or_404(Event, id=event_id)
    """
    get_object_or_404的介绍： 我们原来调用django 的get方法，如果查询的对象不存在的话，会抛出一个DoesNotExist的异常，
    现在我们调用django get_object_or_404方法，它会默认的调用django 的get方法， 如果查询的对象不存在的话，会抛出一个Http404的异常
    """
    return render(request, "sign_index.html", {"event":event})
#签到动作
@login_required
def sign_index_action(request, event_id):
    event = get_object_or_404(Event, id=event_id)
    phone = request.POST.get("phone","")

    result =Guest.objects.filter(phone=phone)
    if not result:
        return render(request, "sign_index.html", {"event":event,"hint":"phone error"})

    result = Guest.objects.filter(phone=phone, event_id=event_id)
    if not result:
        return render(request, "sign_index.html", {"event":event,"hint":"event_id or phone error"})

    result = Guest.objects.get(phone=phone, event_id=event_id)
    if result.sign:
        return render(request, "sign_index.html", {"event":event, "hint":"user has sign in."})
    else:
        Guest.objects.filter(phone=phone, event_id=event_id).update(sign="1")
        return render(request, "sign_index.html", {"event":event, "hint":"sign in suceesss","guest":result})

#[page 100]
#退出登录
def logout(request):
    auth.logout(request)  #退出登录
    response = HttpResponseRedirect("/index")
    return response
